package com.simple.piger.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.simple.piger.utils.PublicUtil;
import com.simple.piger.dto.resp.SysRoleListRespDto;
import com.simple.piger.entity.CmsSysRole;
import com.simple.piger.entity.CmsSysRolePermission;
import com.simple.piger.entity.CmsSysUserRole;
import com.simple.piger.exception.BusinessException;
import com.simple.piger.mapper.CmsSysRoleMapper;
import com.simple.piger.service.CmsSysRolePermissionService;
import com.simple.piger.service.CmsSysRoleService;
import com.simple.piger.service.CmsSysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 服务实现类
 * @Author  donghj
 * @Date    2017/11/20 19:26
 */
@Service
public class CmsSysRoleServiceImpl extends ServiceImpl<CmsSysRoleMapper, CmsSysRole> implements CmsSysRoleService {
    @Autowired
    private CmsSysRolePermissionService cmsSysRolePermissionService;
    @Autowired
    private CmsSysUserRoleService cmsSysUserRoleService;

    @Override
    public int getRoleListCount() {
        return this.baseMapper.selRoleListCount();
    }
    @Override
    public List<SysRoleListRespDto> getRoleList(Integer page, Integer limit) {
        Integer start = PublicUtil.getPageStart(page, limit);
        return this.baseMapper.selRoleList(start, limit);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addRole(CmsSysRole cmsSysRole, String[] rightsAdd) {
	    // 新增角色信息
        cmsSysRole.setCreateDate(new Date());
	    Boolean bool = this.insert(cmsSysRole);
        if (!bool) {
            throw new BusinessException("角色信息新增失败！");
        }
        // 新增角色的权限信息
        if (rightsAdd != null && rightsAdd.length > 0) {
            List<CmsSysRolePermission> add_cmsSysRolePermissions = new ArrayList<>();
            for (int i=0;i<rightsAdd.length;i++) {
                CmsSysRolePermission cmsSysRolePermission = new CmsSysRolePermission();
                cmsSysRolePermission.setRoleId(cmsSysRole.getId());
                cmsSysRolePermission.setPermissionId(Integer.parseInt(rightsAdd[i]));
                add_cmsSysRolePermissions.add(cmsSysRolePermission);
            }
            Boolean bool1 = cmsSysRolePermissionService.insertBatch(add_cmsSysRolePermissions);
            if (!bool1) {
                throw new BusinessException("角色与权限关联信息新增失败！");
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editRole(CmsSysRole cmsSysRole, String[] rightsAdd, String[] rightsDel) {
	    // 修改角色信息
        Boolean bool = this.updateById(cmsSysRole);
        if (!bool) {
            throw new BusinessException("角色信息修改失败！");
        }
        // 新增角色的权限信息
        if (rightsAdd != null && rightsAdd.length > 0) {
            List<CmsSysRolePermission> add_cmsSysRolePermissions = new ArrayList<>();
            for (int i=0;i<rightsAdd.length;i++) {
                CmsSysRolePermission cmsSysRolePermission = new CmsSysRolePermission();
                cmsSysRolePermission.setRoleId(cmsSysRole.getId());
                cmsSysRolePermission.setPermissionId(Integer.parseInt(rightsAdd[i]));
                add_cmsSysRolePermissions.add(cmsSysRolePermission);
            }
            Boolean bool1 = cmsSysRolePermissionService.insertBatch(add_cmsSysRolePermissions);
            if (!bool1) {
                throw new BusinessException("角色与权限关联信息新增失败！");
            }
        }
        // 删除角色的权限信息
        if (rightsDel != null && rightsDel.length > 0) {
            EntityWrapper<CmsSysRolePermission> ew_cmsSysRolePermission = new EntityWrapper<>();
            ew_cmsSysRolePermission.eq("role_id", cmsSysRole.getId());
            ew_cmsSysRolePermission.in("permission_id", Arrays.asList(rightsDel));
            Boolean bool2 = cmsSysRolePermissionService.delete(ew_cmsSysRolePermission);
            if (!bool2) {
                throw new BusinessException("角色与权限关联信息删除失败！");
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delRoleById(Integer id) {
        // 获取当前角色在用户角色关联表里的所有信息
        EntityWrapper<CmsSysUserRole> ew_cmsSysUserRole = new EntityWrapper<>();
        ew_cmsSysUserRole.eq("role_id", id);
        List<CmsSysUserRole> cmsSysUserRoles = cmsSysUserRoleService.selectList(ew_cmsSysUserRole);
        if (cmsSysUserRoles.size() == 0) {
            // 删除角色信息
            Boolean bool = this.deleteById(id);
            if (!bool) {
                throw new BusinessException("角色信息删除失败！");
            }
            // 删除当前角色在用户角色关联表里的所有信息
            if (cmsSysUserRoles.size() > 0) {
                Boolean bool1 = cmsSysUserRoleService.delete(ew_cmsSysUserRole);
                if (!bool1) {
                    throw new BusinessException("用户与角色关联信息删除失败！");
                }
            }
        } else {
            return "当前角色下已有帐号，不可进行删除操作！";
        }
        return null;
    }

    @Override
    public List<CmsSysRole> getRoleListByUserId(String id) {
        return this.baseMapper.selRoleListByUserId(id);
    }
}